package com.qk.management.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.domain.UserDO;
import com.qk.dto.UserDTO;
import com.qk.entity.User;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-08 15:49
 */
@Service
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<UserDO> listPage(UserDTO userDTO) {
        PageHelper.startPage(userDTO.getPage(), userDTO.getPageSize());
        List<UserDO> list = userMapper.listPage(userDTO);
        Page<UserDO> user = (Page<UserDO>) list;
        return PageResult.<UserDO>builder()
                .total(Long.parseLong(user.getTotal() + ""))
                .rows(list)
                .build();
    }

    @Override
    public void deleteByList(List<Integer> ids) {
        userMapper.deleteByList(ids);
    }

    @Override
    public void addUser(User user) {
        // TODO 判重:email邮箱 phone手机 username用户名
        LocalDateTime now = LocalDateTime.now();
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername() + "123").getBytes()));
        user.setCreateTime(now);
        user.setUpdateTime(now);
        userMapper.addUser(user);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void updateById(User user) {
        // TODO 判重:email邮箱 phone手机 username用户名
        LocalDateTime now = LocalDateTime.now();
        user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword() + "123").getBytes()));
        user.setUpdateTime(now);
        userMapper.updateById(user);
    }

    @Override
    public List<UserDO> listAll() {
        return userMapper.listAll();
    }

    @Override
    public List<User> listByRole(String roleLabel) {
        return userMapper.listByRole(roleLabel);
    }

    @Override
    public List<User> listByDept(Integer deptId) {
        return userMapper.listByDept(deptId);
    }
}
